package smartbooks.ui.helper;

import smartbooks.exceptions.MyException;
import smartbooks.ui.util.CommonConstants;
import smartbooksdb.hibernatebean.Invoice;

/**
 *
 * @author Ankit
 */
public class InvoiceHelper implements CommonConstants {
    /**
     * This method is used to validate the invoice panel
     * @param invoice
     * @return 'true' if invoice panel is validated or else 'false'
     * @throws MyException
     */
    public static boolean validateInvoice(Invoice invoice) throws MyException {
        boolean invoicePanelValidated = true;
        
        if(null==invoice.getInvoiceNo() || invoice.getInvoiceNo().length()==0) {
            throw new MyException(INVOICE_NO, "Invoice No cannot be blank");
        }
        
        if(null!=invoice.getInvoiceDiscountType()) {
            switch (invoice.getInvoiceDiscountType()) {
                case "%":
                    if(invoice.getInvoiceDiscountValue()>100) {
                        throw new MyException(INVOICE_DISCOUNT_VALUE, "Discount percentage cannot be greater than 100%");
                    }
                    break;
                case "-":
                    if(invoice.getInvoiceDiscountValue()>(invoice.getInvoiceGrossAmount()+invoice.getInvoiceTax())) {
                        throw new MyException(INVOICE_DISCOUNT_VALUE, "Discount value cannot be greater than Gross Amount + Tax Amount");
                    }
                    break;
            }
        }
        
        if(invoice.getInvoicePaymentAmount()>invoice.getInvoiceNetAmount()) {
            throw new MyException(INVOICE_PAYMENT_AMOUNT, "Payment Amount cannot be greater than Net Amount");
        }
        
        if(null!=invoice.getBillToState() && invoice.getBillToState().length()>32) {
            throw new MyException(BILL_TO_STATE, "Bill To State should have maximum 32 characters");
        }
        
        if(null!=invoice.getBillToCity() && invoice.getBillToCity().length()>32) {
            throw new MyException(BILL_TO_CITY, "Bill To City should have maximum 32 characters");
        }
        
        if(null!=invoice.getShipToState() && invoice.getShipToState().length()>32) {
            throw new MyException(SHIP_TO_STATE, "Ship To State should have maximum 32 characters");
        }
        
        if(null!=invoice.getShipToCity() && invoice.getShipToCity().length()>32) {
            throw new MyException(SHIP_TO_CITY, "Ship To City should have maximum 32 characters");
        }
        
        return invoicePanelValidated;
    }
}